AccountView2 = BaseView:Extend()

function AccountView2:New()
	local o = AccountView2.super.New(self)
	o.abName = "ui_login"
	o.resName = "AccountView2"
	o.layer = ENUM.UI_LAYER.TOP
	--self.hide_pre_view = true
	return o
end

function AccountView2:loadSuccess()
	self.ip_list = { "127.0.0.1:3001", "192.168.1.170:3001" }
	self.label_version = GetNode(self.transform, { "label_version" }, "Text")
	self.Button = GetNode(self.transform, { "Button" }, "GameObject")
	self.if_account = GetNode(self.transform, { "if_account" }, "_InputField")
	self.if_Ip = GetNode(self.transform, { "InputField" }, "_InputField")
	self.Dropdown = GetNode(self.transform, { "Dropdown" }, "_Dropdown")
	CS.UINodeHelper.UpdateDropdownView(self.Dropdown, self.ip_list)


	local save_ip = CookieManager:GetInstance():GetCookie("ip")
	if save_ip then
		Debug.Log("save_ip ==== {0}",save_ip)
		self.if_Ip.text = save_ip
	end

	self:InitEvent()
end

function AccountView2:InitEvent()
	AddClick(self.Button, function()
		if WebSocketManager:GetInstance().connect then
			self:Login()
			return
		end
		local index = self.Dropdown.value + 1
		local address = self.ip_list[index]
		if self.if_Ip.text ~= "" then
			address = self.if_Ip.text
			CookieManager:GetInstance():SetCookie("ip", address)
		end
		WebSocketManager:GetInstance():Connect("ws://" .. address)
	end)

	self:BindEvent(GlobeEvent, Events.CONNECT_SUCCESS, function ()
		self:Login()
	end)

	WebSocketManager:GetInstance():BindProto(L_SCCmd.msgToId["LoginResponse"],self.HandlerLoginResponse , self) 
end

function AccountView2:Login()
	if self.if_account.text == "" then return end
	local msg = "LoginRequest"
	local data = {
		username = self.if_account.text,
		password = "123456",
		loginType = 2,
		sceneType = 2
	}
	WebSocketManager:GetInstance():Request(msg, data)
end

function AccountView2:HandlerLoginResponse(data)
	if data.result == "LOGIN_SUCCESS" then
		UIManager:GetInstance():LoadView("MainView")
	else
		Message:Show(data.result)
	end
end

function AccountView2:OnClose()
	Debug.Log("AccountView2:OnClose")
end

function AccountView2:destroy_callback()
	WebSocketManager:GetInstance():UnBindProto(L_SCCmd.msgToId["LoginResponse"])
end

function AccountView2:__delete()
end
